<template>
    <el-page-header icon="el-icon-arrow-left" @back="$router.replace('/')" content="预定房型"></el-page-header>
    <el-divider></el-divider>
    <el-form :model="formData" ref="myForm" :rules="rules" label-width="100px">
        <el-form-item label="预定人手机" prop="phone">
            <el-input v-model="formData.phone" placeholder="如: 10086"></el-input>
        </el-form-item>
        <el-form-item label="预订人称呼" prop="orderName">
            <el-input v-model="formData.orderName" placeholder="如: 张先生"></el-input>
        </el-form-item>
        <el-form-item label="预定时间" prop="checkTime">
            <el-date-picker
                    v-model="formData.checkTime"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="入住时间"
                    end-placeholder="离店时间" :disabledDate="disabledDate">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="预定方式" prop="orderType">
            <el-select v-model="formData.orderType" placeholder="请选择">
                <el-option
                        v-for="item in orderTypeOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-divider></el-divider>
            <el-button @click="addOrderItem">添加订单项</el-button>
            <el-divider></el-divider>
            <el-form inline v-for="(item, index) in formData.orderbillItems">
                <el-form-item label="预定房型">
                    <el-select v-model="formData.orderbillItems[index].roomTypeId" filterable placeholder="请选择">
                        <el-option
                                v-for="item in roomTypeOptions"
                                :key="item.id"
                                :label="item.typeName"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="预定数量">
                    <el-input-number :min="1" v-model="formData.orderbillItems[index].quantity"></el-input-number>
                </el-form-item>
                <el-form-item>
                    <el-button @click="removeOrderItem(index)">删除</el-button>
                </el-form-item>
            </el-form>


        <el-divider></el-divider>
        <el-form-item>
            <el-button type="primary" @click="submitForm('myForm')">提交</el-button>
            <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script>

    import {ElMessage} from 'element-plus'

    const formatDate = (date) => {
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        return y + '-' + m + '-' + d + " 00:00:00";
    }

    export default {
        data() {
            return {
                formData: {
                    phone: '',
                    orderName: '',
                    orderType: 1,
                    checkTime: [],
                    orderbillItems: [{
                        roomTypeId: '',
                        quantity: 1
                    }]
                },
                orderTypeOptions: [
                    {label: '前台办理', value: 1},
                    {label: '电话预约', value: 2}
                ],
                roomTypeOptions: [],
                rules: {
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'}
                    ],
                    orderName: [
                        {required: true, message: '请输入预定人', trigger: 'blur'}
                    ],
                    checkTime: [
                        {required: true, message: '请输入预定时间', trigger: 'blur'}
                    ],
                    orderType: [
                        {required: true, message: '请输入预定类型', trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (!valid) return false;
                    // 处理日期
                    this.formData.checkInTime = formatDate(this.formData.checkTime[0]);
                    this.formData.checkOutTime = formatDate(this.formData.checkTime[1]);
                    this.axios.post('orderbill/reserve', this.formData)
                        .then(resp => {
                            if (!resp.data.flag) {
                                ElMessage.error(resp.data.msg);
                                return;
                            }
                            ElMessage.success(resp.data.msg);
                            this.$router.replace('/')
                        });
                });
            },
            addOrderItem() {
                this.formData.orderbillItems.push({
                    roomTypeId: this.roomTypeOptions[0].id,
                    checkTime: '',
                    quantity: 1
                });
            },
            removeOrderItem(index) {
                this.formData.orderbillItems.splice(index, 1);
            },
            disabledDate(date) {
                let validDate = new Date();
                validDate.setHours(0);
                validDate.setMinutes(0);
                validDate.setSeconds(0);
                validDate.setMilliseconds(0);
                if (date < validDate) return true;
                validDate.setDate(validDate.getDate() + 20);
                return date >= validDate;
            }
        },
        created() {
            // 查询房型列表
            this.axios.get('roomtype/findOptions')
            .then(resp => {
                if (!resp.data.flag) {
                    ElMessage.error(resp.data.msg);
                    return;
                }
                this.roomTypeOptions = resp.data.data.list;
                this.formData.orderbillItems[0].roomTypeId = this.roomTypeOptions[0].id;
            });
        }
    }
</script>

<style scoped>

</style>